package com.tju.service.impl;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import com.tju.service.MsmService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public boolean sendMsg(String phone, String code) {
        if(StringUtils.isEmpty(phone)){
            return false;
        }
        try {
            Credential credential = new Credential("AKIDzpQlpc8futc5HCpCKCn69vQedMo0wb09"
                    , "tVquhDSRAcojNkqfDUUDfpjMinbfSyC9");
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = new SmsClient(credential, "ap-nanjing", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumber = {"86" + phone};
            req.setPhoneNumberSet(phoneNumber);

            req.setSmsSdkAppid("1400623889");
            req.setSign("慢慢懂gsc公众号");
            req.setTemplateID("1278002");

            String[] templateParamSet1 = {code};
            req.setTemplateParamSet(templateParamSet1);

            // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
            SendSmsResponse resp = client.SendSms(req);

            System.out.println(resp);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }

}
